import { Collection, EntitySchema, Ref } from '@mikro-orm/core';
import { JobProjectJobs } from './JobProjectJobs';
import { JobProjectSources } from './JobProjectSources';
import { JobTasksPictures } from './JobTasksPictures';
import { OrderJobEnrolls } from './OrderJobEnrolls';
import { Users } from './Users';

export class JobTasks {
  id!: string;
  taskRefId?: string;
  creator?: Ref<Users>;
  owner?: Ref<Users>;
  source?: Ref<JobProjectSources>;
  createdAt?: Date;
  updatedAt?: Date;
  isRecommend?: Date;
  title?: string;
  content?: unknown;
  longitude?: string;
  latitude?: string;
  locationName?: string;
  address?: string;
  province?: string;
  city?: string;
  district?: string;
  qrcodeUrl?: string;
  completedAt?: Date;
  canceledAt?: Date;
  startDate?: string;
  lastEnrollTime?: string;
  isUpSign?: boolean;
  isDownSign?: boolean;
  allowResign?: boolean;
  allowAgentSign?: boolean;
  isNotify?: boolean = false;
  isPublic?: boolean = false;
  priceType?: string;
  price?: number;
  settleType?: string;
  staffNum: number = 0;
  jobs = new Collection<JobProjectJobs>(this);
  pictures = new Collection<JobTasksPictures>(this);
  enrolls = new Collection<OrderJobEnrolls>(this);
}

export const JobTasksSchema = new EntitySchema({
  class: JobTasks,
  properties: {
    id: { primary: true, type: 'string', columnType: 'bigint' },
    taskRefId: { type: 'string', columnType: 'bigint', nullable: true },
    creator: {
      reference: 'm:1',
      entity: () => Users,
      ref: true,
      onUpdateIntegrity: 'cascade',
      onDelete: 'cascade',
      nullable: true,
      index: 'tasks_creator_forgein_key',
    },
    owner: {
      reference: 'm:1',
      entity: () => Users,
      ref: true,
      onUpdateIntegrity: 'cascade',
      onDelete: 'cascade',
      nullable: true,
      index: 'owner_forgein_key',
    },
    source: {
      reference: 'm:1',
      entity: () => JobProjectSources,
      ref: true,
      onUpdateIntegrity: 'cascade',
      onDelete: 'cascade',
      nullable: true,
      index: 'souce_id',
    },
    createdAt: { type: 'Date', length: 0, nullable: true },
    updatedAt: { type: 'Date', length: 0, nullable: true },
    isRecommend: { type: 'Date', length: 0, nullable: true },
    title: { type: 'string', length: 255, nullable: true },
    content: { type: 'unknown', columnType: 'longtext', nullable: true },
    longitude: { type: 'string', length: 255, nullable: true },
    latitude: { type: 'string', length: 255, nullable: true },
    locationName: { type: 'string', length: 255, nullable: true },
    address: { type: 'string', length: 255, nullable: true },
    province: { type: 'string', length: 255, nullable: true },
    city: { type: 'string', length: 255, nullable: true },
    district: { type: 'string', length: 255, nullable: true },
    qrcodeUrl: { type: 'string', length: 255, nullable: true },
    completedAt: { type: 'Date', length: 0, nullable: true },
    canceledAt: { type: 'Date', length: 0, nullable: true },
    startDate: { type: 'string', columnType: 'date', nullable: true },
    lastEnrollTime: { type: 'string', columnType: 'time', nullable: true },
    isUpSign: { type: 'boolean', nullable: true },
    isDownSign: { type: 'boolean', nullable: true },
    allowResign: { type: 'boolean', nullable: true },
    allowAgentSign: { type: 'boolean', nullable: true },
    isNotify: { type: 'boolean', nullable: true, default: false },
    isPublic: { type: 'boolean', nullable: true, default: false },
    priceType: { type: 'string', length: 10, nullable: true },
    price: { type: 'number', columnType: 'float', nullable: true },
    settleType: { type: 'string', length: 10, nullable: true },
    staffNum: { primary: true, type: 'number', default: 0 },
    jobs: { reference: '1:m', entity: () => JobProjectJobs, mappedBy: 'task' },
    pictures: { reference: '1:m', entity: () => JobTasksPictures, mappedBy: 'task' },
    enrolls: { reference: '1:m', entity: () => OrderJobEnrolls, mappedBy: 'jobTask' },
  },
});
